package com.studymaster.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;
import com.studymaster.domain.SmallPaper;
import com.studymaster.service.SmallPaperService;


@Controller
public class SmallPaperController {
    
	@Autowired
	private SmallPaperService mSmallPaperService;
	
	private static final int  GROUPSEND_SUCCESS= 1;//群发成功
	private static final int  GROUPSEND_FAILIRE= 0;//群发失败
	private static final int  SENDPAPER_SUCCESS = 1;//发送成功
	private static final int  SENDPAPER_FAILIRE = 1;//发送失败
    private static final int  NO_NEWPAPERS = 0;//没有新的小纸条
    private static final int  HAS_NEWPAPERS = 1;//有新的小纸条
	
	@RequestMapping(value = "/receiveallsp.do", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	public ModelAndView receiveAllsmallpaper(@RequestParam("receiverid") String receiverid,
			@RequestParam("date") String date,HttpServletRequest request, ModelMap model) {
		
		List<SmallPaper> smallpapers = mSmallPaperService.getPaperByReceiverId(receiverid, date);
		if(smallpapers.size()!=0){
			model.put("errorcode", HAS_NEWPAPERS);
			model.put("smallpapers", smallpapers );
		}else{
			model.put("errorcode", NO_NEWPAPERS);
		}

		return new ModelAndView(new MappingJacksonJsonView(), model);
	}
	
	@RequestMapping(value = "/receivespinchatform.do", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	public ModelAndView receiveAllsmallpaper(@RequestParam("senderid") String senderid,@RequestParam("receiverid") String receiverid,
			@RequestParam("date") String date,HttpServletRequest request, ModelMap model) {
		
		List<SmallPaper> smallpapers = mSmallPaperService.getPapersFromDB(senderid, receiverid, date);
		if(smallpapers.size()!=0){
			model.put("errorcode", HAS_NEWPAPERS);
			model.put("smallpapers", smallpapers );
		}else{
			model.put("errorcode", NO_NEWPAPERS);
		}
		
		return new ModelAndView(new MappingJacksonJsonView(), model);
	}
	
	
	@RequestMapping(value = "/groupsendpapers.do", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	public ModelAndView groupSendPapers(@RequestParam("senderid") String senderid,
			@RequestParam("sendername") String sendername,@RequestParam("papermessage") String papermessage,
			@RequestParam("receiverid") String receiverid,@RequestParam("type") String type,
			HttpServletRequest request, ModelMap model){
		boolean flag = true;
		String mSenderName = "";
		String mPaperMessage = "";
		String mReceiverid = "";
		String mSenderid = "";
		String mType = "";
		try {
			mSenderid =  URLDecoder.decode(senderid, "UTF-8");
			mSenderName =  URLDecoder.decode(sendername, "UTF-8");
			mPaperMessage = URLDecoder.decode(papermessage, "UTF-8");
			mReceiverid = URLDecoder.decode(receiverid, "UTF-8");
			mType = URLDecoder.decode(type, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String rId[] = mReceiverid.split(",");
		for(int i =0;i<rId.length;i++){
			SmallPaper sp = new SmallPaper();
			sp.setSenderId(mSenderid);
			sp.setSenderName(mSenderName);
			sp.setPaperMessage(mPaperMessage);
			sp.setReceiverId(rId[i]);
			sp.setDate(String.valueOf(System.currentTimeMillis()));
			sp.setType(Integer.valueOf(mType));
			flag = mSmallPaperService.insertSmallPaper(sp);
		}
		if(!flag) {
			model.put("errorcode", GROUPSEND_FAILIRE);
		}else{
			model.put("errorcode", GROUPSEND_SUCCESS);
			model.put("date",String.valueOf(System.currentTimeMillis()));
		}
		return new ModelAndView(new MappingJacksonJsonView(), model);
	}
	
	@RequestMapping(value = "/sendsmallpaper.do", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	public ModelAndView sendsmallpaper(@RequestParam("senderid") String senderid,
			@RequestParam("sendername") String sendername,@RequestParam("papermessage") String papermessage,
			@RequestParam("receiverid") String receiverid,@RequestParam("type") String type,
			HttpServletRequest request, ModelMap model){
		boolean flag = true;
		String mSenderName = "";
		String mPaperMessage = "";
		String mReceiverid = "";
		String mSenderid = "";
		String mType = "";
		try {
			mSenderid =  URLDecoder.decode(senderid, "UTF-8");
			mSenderName =  URLDecoder.decode(sendername, "UTF-8");
			mPaperMessage = URLDecoder.decode(papermessage, "UTF-8");
			mReceiverid = URLDecoder.decode(receiverid, "UTF-8");
			mType = URLDecoder.decode(type, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SmallPaper sp = new SmallPaper();
		sp.setSenderId(mSenderid);
		sp.setSenderName(mSenderName);
		sp.setPaperMessage(mPaperMessage);
		sp.setReceiverId(mReceiverid);
		sp.setDate(String.valueOf(System.currentTimeMillis()));
		sp.setType(Integer.valueOf(mType));
		flag = mSmallPaperService.insertSmallPaper(sp);
		if(!flag) {
			model.put("errorcode", SENDPAPER_FAILIRE);
		}else{
			model.put("errorcode", SENDPAPER_SUCCESS);
			model.put("date",String.valueOf(System.currentTimeMillis()));
		}
		return new ModelAndView(new MappingJacksonJsonView(), model);
	}
	
	
}
